﻿namespace Cubezicles.UI
{
    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    public class HireNewEmployeeManager : ManagerPane
    {
        private EmployeeManager em;

        Texture2D devHead, devBody, leadDevHead, leadDevBody, slackerHead, slackerBody, steveHead, steveBody;

        public HireNewEmployeeManager(Game1 game, EmployeeManager empMan)
            : base(game)
        {
            em = empMan;

            const int xPos = 50;
            const int yPos = 75;
            int w = game.screenW - 100;
            const int h = 430;

            PanePosition = new Vector2(xPos, yPos);
            PaneSize = new Vector2(w, h);
            FrameTitle = game.Messages["NewHirePaneTitle"];

            this.DrawOrder = (int)Game1.DisplayOrder.UI;

        }

        
        protected override void LoadContent()
        {
            /* hireEmp = new Button()
               {
                   Texture = butts,
                   text = "Hire new employee!",
                   xpos = game.screenW - 230,
                   ypos = 50 + 20,
                   // Close the panel (by continuing time
                   OnClick = (cont) => e.Hire()
               };
               */
            //Consider getting the faces from the thinghy thing below
            devHead = Game.Content.Load<Texture2D>("Models/Employees/Dev/face");
            devBody = Game.Content.Load<Texture2D>("Models/Employees/Dev/body_front2");
            leadDevHead = Game.Content.Load<Texture2D>("Models/Employees/LeadDev/face");
            leadDevBody = Game.Content.Load<Texture2D>("Models/Employees/LeadDev/body_front2");
            slackerHead = Game.Content.Load<Texture2D>("Models/Employees/Slacker/face");
            slackerBody = Game.Content.Load<Texture2D>("Models/Employees/Slacker/body_front2");
            steveHead = Game.Content.Load<Texture2D>("Models/Employees/Steve/face");
            steveBody = Game.Content.Load<Texture2D>("Models/Employees/Steve/body_front2");
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            if (IsVisible)
            {
                // Make sure the other employee pane isn't showing... problems with clicking through pane
                em.managerPane.IsVisible = false; 

                // Create a list of UI controls
                controls = new List<Button>(); // variable in base class

                // Get the screen width and height
                int screenH = GraphicsDevice.Viewport.Height;
                int screenW = GraphicsDevice.Viewport.Width;

                game.SpriteBatch.Begin();

                // Draw the frame that hold the info
                DrawFrame();
                
                
                // List all the employee hire listings
                int y = 105;
                int x = 140;
                const int lineHeight = 90;

                DrawNewHireListing(EmployeeManager.EmployeeType.Dev, x, y);
                //x += spaceBtw;
                y += lineHeight;
                DrawNewHireListing(EmployeeManager.EmployeeType.LeadDev, x, y);
                y += lineHeight;
                //x += spaceBtw;
                DrawNewHireListing(EmployeeManager.EmployeeType.Slacker, x, y);
                y += lineHeight;
                //x += spaceBtw;
                DrawNewHireListing(EmployeeManager.EmployeeType.Steve, x, y);
                y += lineHeight;
                //x += spaceBtw;
                //DrawNewHireListing(EmployeeManager.EmployeeType.UXDesigner, x, y);
                //y += lineHeight;


                // Draw the okay button
                int xpos = x + (screenW/2 - 120)/2 - 20;
                int ypos = y;
                DrawOKbutton(xpos, ypos);


                // Draw ALL the buttons
                controls.ForEach(control => control.Update(GraphicsDevice.Viewport, game.ms, game.oldms));
                controls.ForEach(control => control.Draw(game.SpriteBatch, GraphicsDevice.Viewport));
                
                game.SpriteBatch.End();

            }
        }

        private void DrawNewHireListing(EmployeeManager.EmployeeType employeeType, int x, int y)
        {
            // Get the screen width and height
            int screenW = GraphicsDevice.Viewport.Width;
            bool canHire = true;

            // Employee emp = null;

            Texture2D face, body;

            String name = ""; // = emp.name;
            String tagline = ""; //emp.tagline;
            // int price = emp.price;
            // should also list salary
            int hireBonusCost = 0;

            // this stuff should all be gotten from objects, but testing now...
            //Consider using pure textures?
            switch(employeeType)
            {
                case EmployeeManager.EmployeeType.Dev:
                    name = "Developer";
                    hireBonusCost = MoneyManager.CostHireBonusDev;
                    tagline = game.Messages["DevHiringTagline"];
                    face = devHead;
                    body = devBody;
                    break;
                case EmployeeManager.EmployeeType.LeadDev:
                    name = "Lead Developer";
                    hireBonusCost = MoneyManager.CostHireBonusLeadDev;
                    tagline = game.Messages["LeadDevHiringTagline"];
                    face = leadDevHead;
                    body = leadDevBody;
                    break;
                case EmployeeManager.EmployeeType.Slacker:
                    //emp = em.slacker;
                    name = "Slacker";
                    hireBonusCost = MoneyManager.CostHireBonusSlacker;
                    tagline = game.Messages["SlackerHiringTagline"];
                    face = slackerHead;
                    body = slackerBody;
                    break;
                case EmployeeManager.EmployeeType.Steve:
                    name = "Steve";
                    hireBonusCost = MoneyManager.CostHireBonusSteve;
                    tagline = game.Messages["SteveHiringTagline"];
                    face = steveHead;
                    body = steveBody;
                    break;
                default:
                    face = devHead;
                    body = devBody;
                    break;
            }

            // -- todo: change these back from actual numbers to constants or relative values

            // Draw new hire listing
            // Draw the frame that hold the info
            game.drawFrame(x, y, 500, 80, name, game.Fonts["tiny"]);

            // check for sufficient funds and under employee limit
            if (em.NextEmployeeID > 9 || game.MoneyManager.CurrentFunds < MoneyManager.CostHireBonus)
            {
                canHire = false;                
                //hireEmp.Disable(); // Disable the button - player can't hire employee


                // Display why the player can't hire an employee.

                // If there were not sufficient funds, display a notification to the user //if (game.MoneyManager.CurrentFunds < MoneyManager.CostHireBonus)
                string err = "Can't afford!";
                Color errColor = Color.Red; 

                // Check for this one second, vs. vice versa, because if a player already
                // has 10 employees and doesn't have sufficient funds to hire, the reason the hire button
                // is disabled is because they've reached the max, not because of funds
                if (em.Employees.Count >= 9)
                {
                    err = "Max Capacity!"; // Player has reached the maximum number of employees
                    errColor = Color.Gray;
                }

                // location to draw reason, should be right next to buy button
                int reasonErrorX = 470; /*x + screenW/2 - 130 - 60 - (int)game.Fonts["info"].MeasureString(err).X;*/
                int reasonErrorY = y + 55;

                game.SpriteBatch.DrawString(game.Fonts["info"], err, new Vector2(reasonErrorX, reasonErrorY), errColor); 
            }

            // Draw the Hire button
            DrawHireButton(employeeType, 550, y+49, screenW, canHire, hireBonusCost);

            SpriteFont font = game.Fonts["tiny"];
            Color color = game.UIblue; //text color

            // Draw picture representation

            // Draw avatar image
            int headw = 45, bodyw = 30, aviY = (int)y;
            float proportion = 1.3f;

            game.SpriteBatch.Draw(face, new Rectangle(x - bodyw + (bodyw - headw) / 2 - 30, aviY, headw, headw), Color.White);
            
            game.SpriteBatch.Draw(body,
                new Rectangle(x - bodyw - 30, aviY - 5 + headw, bodyw, (int)(bodyw * proportion)),
                new Rectangle(0, 0, devBody.Width, (int)(body.Width * proportion)),
            Color.White);


            // x, y position to write tagline
            int tagX = 150; //x + screenW / 2 - 130 - (int)font.MeasureString(tagline).X;
            int tagY = y + 23;

            // Write Tagline to screen
            game.SpriteBatch.DrawString(font, tagline, new Vector2(tagX, tagY), color);

            string priceStr = "$" + hireBonusCost;
            // x, y position to write price
            int pX = 575; /*x + screenW / 2 - 130 - (int)font.MeasureString(priceStr).X;*/
            int pY = y + 34; /*38;*/

            // Write Price to screen
            game.SpriteBatch.DrawString(font, priceStr, new Vector2(pX, pY), color);
        }

        private void DrawHireButton(EmployeeManager.EmployeeType employeeType, int x, int y, int screenW, bool canHire, int price)
        {
            // Draw the Hire button
            var buyButton = new Button()
            {
                Texture = butts,
                font = game.Fonts["info"],
                text = "Hire 'em!",
                xpos = x,
                ypos = y,
                Active = canHire,
                OnClick = (cont) =>
                {
                    // Check if can afford item
                    if (game.MoneyManager.Pay(price))
                    {
                        em.Hire(employeeType);
                    }
                }
            };
            controls.Add(buyButton);
        }

        // not sure how to override... but we want to additionally show the original employeemanagerpane
        // when we close window/press okay, not just hide this pane
        public override void ClosePane()
        {
            // -- from base -- //
            IsVisible = false;
            em.managerPane.IsVisible = true;
        }
    }
}